/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.SOP;

import helper.FigureItemLine;
import helper.SalesChartItemOnePeriod;
import helper.SalesChartItemTotal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import merlion.common.util.Consts;
import merlion.crm.sopm.session.SOPSessionBeanLocal;

/**
 *
 * @author Zhang Ying
 */
@ManagedBean
@ViewScoped
public class SalesFiguireManagerBean {

    public static final String FIRST_HALF = "Jan-June";
    public static final String SECOND_HALF = "July-Dec";
    @EJB
    private SOPSessionBeanLocal sopManager;
    public ArrayList<String> periods;
    private String selectedPeriod;
    private HashMap qtyOfBars;
    private HashMap revenues;
    private FigureItemLine labelLine;
    // paths defined
    private String sopPath = "/SOP/";

    /** Creates a new instance of SalesFiguireManagerBean */
    public SalesFiguireManagerBean() {
        // populate the periods information
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        String monthStr = sdf.format(date);
        sdf = new SimpleDateFormat("yyyy");
        String yearStr = sdf.format(date);
        periods = getPeriodInfo(monthStr, yearStr);
        selectedPeriod = periods.get(0);
        // populate all quantity information to 2-dimension list, x: bar 0,1,2,3; y:0:all, 1-12 months
    }

    // for choosing another period
    public String chooseAnotherPeriod() {
        return getRedirectPath(sopPath, "getAllSalesFigures") + "&period=" + selectedPeriod;
    }

    public Collection<FigureItemLine> getFigures(String periodName) {
        System.out.println("FOR debugging: " + periodName);
        Collection<FigureItemLine> result = new ArrayList<FigureItemLine>();
        // trim period name
        String[] trims = periodName.split(" ");
        String yearStr = trims[1];
        Long yearKey = new Long(Long.valueOf(yearStr));
        if (trims[0].equals("Jan-June")) {
            labelLine = new FigureItemLine("", Consts.JAN, Consts.FEB, Consts.MAR, Consts.APR, Consts.MAY, Consts.JUN);
        } else {
            labelLine = new FigureItemLine("", Consts.JUL, Consts.AUG, Consts.SEP, Consts.OCT, Consts.NOV, Consts.DEC);
        }

        for (int i = 0; i < 4; i++) {
            HashMap yearMap = (HashMap) getQtyOfBars().get(new Long(i));
            ArrayList<Long> months12 = (ArrayList<Long>) yearMap.get(yearKey);
            String label = "";
            switch (i) {
                case 0:
                    label = Consts.ProductType.BAR_A.getText();
                    break;
                case 1:
                    label = Consts.ProductType.BAR_B.getText();
                    break;
                case 2:
                    label = Consts.ProductType.BAR_C.getText();
                    break;
                case 3:
                    label = Consts.ProductType.BAR_D.getText();
                    break;
            }
            FigureItemLine temp = new FigureItemLine(label,
                    months12.get(labelLine.getKeyLongByName(labelLine.getCol2())).toString(),
                    months12.get(labelLine.getKeyLongByName(labelLine.getCol3())).toString(),
                    months12.get(labelLine.getKeyLongByName(labelLine.getCol4())).toString(),
                    months12.get(labelLine.getKeyLongByName(labelLine.getCol5())).toString(),
                    months12.get(labelLine.getKeyLongByName(labelLine.getCol6())).toString(),
                    months12.get(labelLine.getKeyLongByName(labelLine.getCol7())).toString());
            result.add(temp);
        }
        return result;
    }

    public Collection<FigureItemLine> getRevenueFigures(String periodName) {
        System.out.println("FOR debugging: " + periodName);
        Collection<FigureItemLine> result = new ArrayList<FigureItemLine>();
        // trim period name
        String[] trims = periodName.split(" ");
        String yearStr = trims[1];
        Long yearKey = new Long(Long.valueOf(yearStr));
        if (trims[0].equals("Jan-June")) {
            labelLine = new FigureItemLine("", Consts.JAN, Consts.FEB, Consts.MAR, Consts.APR, Consts.MAY, Consts.JUN);
        } else {
            labelLine = new FigureItemLine("", Consts.JUL, Consts.AUG, Consts.SEP, Consts.OCT, Consts.NOV, Consts.DEC);
        }

        for (int i = 0; i < 4; i++) {
            HashMap yearMap = (HashMap) getRevenues().get(new Long(i));
            ArrayList<Long> months12 = (ArrayList<Long>) yearMap.get(yearKey);
            String label = "";
            switch (i) {
                case 0:
                    label = Consts.ProductType.BAR_A.getText();
                    break;
                case 1:
                    label = Consts.ProductType.BAR_B.getText();
                    break;
                case 2:
                    label = Consts.ProductType.BAR_C.getText();
                    break;
                case 3:
                    label = Consts.ProductType.BAR_D.getText();
                    break;
            }
            FigureItemLine temp = new FigureItemLine(label,
                    months12.get(labelLine.getKeyLongByName(labelLine.getCol2())).toString(),
                    months12.get(labelLine.getKeyLongByName(labelLine.getCol3())).toString(),
                    months12.get(labelLine.getKeyLongByName(labelLine.getCol4())).toString(),
                    months12.get(labelLine.getKeyLongByName(labelLine.getCol5())).toString(),
                    months12.get(labelLine.getKeyLongByName(labelLine.getCol6())).toString(),
                    months12.get(labelLine.getKeyLongByName(labelLine.getCol7())).toString());
            result.add(temp);
        }
        return result;
    }

    public Collection<SalesChartItemOnePeriod> getRevenueChartFigure(String periodName) {
        ArrayList<SalesChartItemOnePeriod> result = new ArrayList<SalesChartItemOnePeriod>();
        String[] monthLabel = new String[6];
        boolean firstHalf;
        String[] trims = periodName.split(" ");
        String yearStr = trims[1];
        Long yearKey = Long.valueOf(yearStr);

        if (trims[0].equals("Jan-June")) {
            firstHalf = true;
            monthLabel[0] = Consts.JAN;
            monthLabel[1] = Consts.FEB;
            monthLabel[2] = Consts.MAR;
            monthLabel[3] = Consts.APR;
            monthLabel[4] = Consts.MAY;
            monthLabel[5] = Consts.JUN;
        } else {
            firstHalf = false;
            monthLabel[0] = Consts.JUL;
            monthLabel[1] = Consts.AUG;
            monthLabel[2] = Consts.SEP;
            monthLabel[3] = Consts.OCT;
            monthLabel[4] = Consts.NOV;
            monthLabel[5] = Consts.DEC;
        }

        for (int j = 0; j < 6; j++) {
            SalesChartItemOnePeriod chartLine = new SalesChartItemOnePeriod(monthLabel[j], Long.valueOf(0), Long.valueOf(0), Long.valueOf(0), Long.valueOf(0));
            result.add(chartLine);
        }

        for (int i = 0; i < 4; i++) {
            HashMap yearMap = (HashMap) getRevenues().get(new Long(i));
            ArrayList<Long> months12 = (ArrayList<Long>) yearMap.get(yearKey);
            String label = "";
            switch (i) {
                case 0:
                    label = Consts.ProductType.BAR_A.getText();
                    for (int j = 0; j < 6; j++) {
                        SalesChartItemOnePeriod temp = result.get(j);
                        if (firstHalf) {
                            temp.setSalesRevPerMon1(months12.get(j + 1));
                        } else {
                            temp.setSalesRevPerMon1(months12.get(j + 7));
                        }
                    }
                    break;
                case 1:
                    label = Consts.ProductType.BAR_B.getText();
                    for (int j = 0; j < 6; j++) {
                        SalesChartItemOnePeriod temp = result.get(j);
                        if (firstHalf) {
                            temp.setSalesRevPerMon2(months12.get(j + 1));
                        } else {
                            temp.setSalesRevPerMon2(months12.get(j + 7));
                        }
                    }
                    break;
                case 2:
                    label = Consts.ProductType.BAR_C.getText();
                    for (int j = 0; j < 6; j++) {
                        SalesChartItemOnePeriod temp = result.get(j);
                        if (firstHalf) {
                            temp.setSalesRevPerMon3(months12.get(j + 1));
                        } else {
                            temp.setSalesRevPerMon3(months12.get(j + 7));
                        }
                    }
                    break;
                case 3:
                    label = Consts.ProductType.BAR_D.getText();
                    for (int j = 0; j < 6; j++) {
                        SalesChartItemOnePeriod temp = result.get(j);
                        if (firstHalf) {
                            temp.setSalesRevPerMon4(months12.get(j + 1));
                        } else {
                            temp.setSalesRevPerMon4(months12.get(j + 7));
                        }
                    }
                    break;
            }
        }
        return result;
    }

    public Collection<SalesChartItemTotal> getRevenueYearFigure(String periodName) {
        ArrayList<SalesChartItemTotal> result = new ArrayList<SalesChartItemTotal>();
        
        for (int j = Consts.BASE_YEAR; j < Consts.BASE_YEAR + periods.size(); j++) {
            SalesChartItemTotal temp = new SalesChartItemTotal(String.valueOf(j), 0, 0, 0, 0);
            for (int i = 0; i < 4; i++) {
                HashMap yearMap = (HashMap) getRevenues().get(new Long(i));
                ArrayList<Long> months12 = (ArrayList<Long>) yearMap.get(Long.valueOf(j));
                String label;
                switch (i) {
                    case 0:
                        label = Consts.ProductType.BAR_A.getText();
                        temp.setQa(months12.get(0));
                        break;
                    case 1:
                        label = Consts.ProductType.BAR_B.getText();
                        temp.setQb(months12.get(0));
                        break;
                    case 2:
                        label = Consts.ProductType.BAR_C.getText();
                        temp.setQc(months12.get(0));
                        break;
                    case 3:
                        label = Consts.ProductType.BAR_D.getText();
                        temp.setQd(months12.get(0));
                        break;
                }
            }
            result.add(temp);
        }

        return result;
    }

    // private helper methods
    private String getRedirectPath(String parentPath, String actionPath) {
        return parentPath + actionPath + "?faces-redirect=true";
    }

    private ArrayList<String> getPeriodInfo(String currentMonth, String currentYear) {
        int month = Integer.valueOf(currentMonth);
        int year = Integer.valueOf(currentYear);
        ArrayList<String> result = new ArrayList<String>();
        for (int i = year; i >= Consts.BASE_YEAR; i--) {
            String info = new Integer(i).toString();
            if (i == year) {
                if (month <= 6) {
                    result.add(FIRST_HALF + " " + info);
                } else {
                    result.add(SECOND_HALF + " " + info);
                    result.add(FIRST_HALF + " " + info);
                }
            } else {
                result.add(SECOND_HALF + " " + info);
                result.add(FIRST_HALF + " " + info);
            }
        }
        return result;
    }

    // getters and setters
    public ArrayList<String> getPeriods() {
        return periods;
    }

    public void setPeriods(ArrayList<String> periods) {
        this.periods = periods;
    }

    public String getSelectedPeriod() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String parameter = request.getParameter("period");
        if (parameter != null) {
            selectedPeriod = parameter;
        }
        return selectedPeriod;
    }

    public void setSelectedPeriod(String selectedPeriod) {
        this.selectedPeriod = selectedPeriod;
    }

    public HashMap getQtyOfBars() {
        if (qtyOfBars == null) {
            qtyOfBars = sopManager.getSalesFiguireLstOfLst(periods.size());
        }
        return qtyOfBars;
    }

    public void setQtyOfBars(HashMap qtyOfBars) {
        this.qtyOfBars = qtyOfBars;
    }

    public HashMap getRevenues() {
        if (revenues == null) {
            revenues = sopManager.getSalesRevenueFiguresLstOfLst(periods.size());
        }
        return revenues;
    }

    public void setRevenues(HashMap revenues) {
        this.revenues = revenues;
    }

    public FigureItemLine getLabelLine() {
        String[] trims = selectedPeriod.split(" ");
        if (trims[0].equals("Jan-June")) {
            labelLine = new FigureItemLine("", Consts.JAN, Consts.FEB, Consts.MAR, Consts.APR, Consts.MAY, Consts.JUN);
        } else {
            labelLine = new FigureItemLine("", Consts.JUL, Consts.AUG, Consts.SEP, Consts.OCT, Consts.NOV, Consts.DEC);
        }
        return labelLine;
    }

    public void setLabelLine(FigureItemLine labelLine) {
        this.labelLine = labelLine;
    }
}
